package com.xyy.scaffoldservice.impl;

import com.xyy.scaffolddao.dataobject.MovieActorDO;
import com.xyy.scaffolddao.dataobject.MovieDetailDO;
import com.xyy.scaffolddao.mapper.MovieActorMapper;
import com.xyy.scaffolddao.mapper.MovieDetailMapper;
import com.xyy.scaffolddao.mapper.MovieMapper;
import com.xyy.scaffolddao.vo.MovieDetailVO;
import com.xyy.scaffolddao.vo.MovieListVO;
import com.xyy.scaffolddao.vo.MovieMainVO;
import com.xyy.scaffolddao.vo.MovieVO;
import com.xyy.scaffoldservice.MovieService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MovieServiceImpl implements MovieService {

    //@Resource
    //MovieMapper movieMapper;

    @Resource
    MovieDetailMapper movieDetailMapper;

    @Resource
    MovieActorMapper movieActorMapper;

    @Override
    public List<MovieVO> getMovieList() {
        List<MovieMainVO> movieMainList = movieDetailMapper.getMovieList();
        if (CollectionUtils.isEmpty(movieMainList)) {
            return null;
        }

//        movieDetailMapper.sout
        System.out.println(movieDetailMapper);

        List<MovieVO> movieList = new ArrayList<>();
        List<String> categoryList = movieMainList.stream().map(MovieMainVO::getMovieCategory).collect(Collectors.toList());
        categoryList = categoryList.stream().distinct().collect(Collectors.toList());
        for (String category : categoryList) {
            List<MovieMainVO> mainList = movieMainList.stream().filter(item -> item.getMovieCategory().equals(category)).collect(Collectors.toList());
            MovieVO movieVO = new MovieVO();
            movieVO.setSubjectTitle(category);
            movieVO.setMovieList(mainList);
            movieList.add(movieVO);
        }

        return movieList;
    }

    @Override
    public List<MovieListVO> selectMovieList() {
        List<MovieDetailDO> movieDetailDOList = movieDetailMapper.selectMovieList();
        if (CollectionUtils.isEmpty(movieDetailDOList)) {
            return null;
        }

        List<MovieListVO> movieList = new ArrayList<>();
        List<String> subjectList = movieDetailDOList.stream().map(MovieDetailDO::getMovieSubject).collect(Collectors.toList());
        subjectList = subjectList.stream().distinct().collect(Collectors.toList());
        for (String subject : subjectList) {
            List<MovieDetailDO> mainList = movieDetailDOList.stream().filter(item -> item.getMovieSubject().equals(subject)).collect(Collectors.toList());
            MovieListVO movieVO = new MovieListVO();
            movieVO.setSubjectTitle(subject);
            movieVO.setMovieList(mainList);
            movieList.add(movieVO);
        }

        return movieList;
    }

    @Override
    public List<MovieDetailDO> selectMoreMovieList() {
        return movieDetailMapper.selectMoreMovieList();
    }

    @Override
    public MovieDetailVO selectMovieDetail() {
        MovieDetailDO movieDetailDO = movieDetailMapper.selectMovieDetail();
        List<MovieActorDO> movieActorList = movieActorMapper.selectMovieActorList();
        MovieDetailVO movieDetailVO = new MovieDetailVO();
        BeanUtils.copyProperties(movieDetailDO, movieDetailVO);
        movieDetailVO.setActorList(movieActorList);

        return movieDetailVO;
    }
}
